package com.codeshogun.android.swipesample;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class Main extends Activity {
	private ListView listView;
	private List<ContextMenuViewtable> items;
	
    private ListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        items = Arrays.asList(new ContextMenuViewtable("chau", false), 
        		new ContextMenuViewtable("hai", false), 
        		new ContextMenuViewtable("nguyen", false), 
        		new ContextMenuViewtable("pham", false));
        adapter = new ListAdapter();
        this.listView = new ListView(getApplicationContext());
        this.listView.setAdapter(adapter);
        setContentView(listView);
        
    }
    
    private class ListAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return items.size();
		}

		@Override
		public Object getItem(int position) {
			return items.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ContextMenuViewtable item = items.get(position);
			if (convertView == null) {
				convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.activity_list_item, null);
				convertView.setOnTouchListener(gestureListener);
			}
			TextView title = (TextView)convertView.findViewById(R.id.title);
			ActivityViewFlipper viewFlipper = (ActivityViewFlipper)convertView.findViewById(R.id.flipper);
			title.setText(item.title);
			if (item.visible) {
				viewFlipper.reset();
			}
			convertView.setTag(item);
			return convertView;
		}
		
		View.OnTouchListener gestureListener = new View.OnTouchListener() {
	    	float downXValue = 0;
	        long downTime = 0;
	        public boolean onTouch(View v, MotionEvent event) {
	        	ActivityViewFlipper viewFlipper = (ActivityViewFlipper)v.findViewById(R.id.flipper);
	        	switch (event.getAction()) {
	        	
	            case MotionEvent.ACTION_DOWN: 
	                downXValue = event.getX();
	                downTime = event.getEventTime();
	                break;

	            case MotionEvent.ACTION_UP:
	                float currentX = event.getX();
	                long currentTime = event.getEventTime();
	                float difference = Math.abs(downXValue - currentX);
	                long time = currentTime - downTime;

	                Log.d("DEBUG",  "Distance: " + difference + "px Time: " + time + "ms");

	                if ( (downXValue < currentX) && (time > 50) && (difference > 50)) {
	                	clearContextMenu();
	                	viewFlipper.showPrevious();
	                	ContextMenuViewtable item = (ContextMenuViewtable)v.getTag();
	                    item.visible = true;
	                }

	                if ( (downXValue > currentX) && (time > 50) && (difference > 50)) {
	                	clearContextMenu();
	                	viewFlipper.showNext();
	                	ContextMenuViewtable item = (ContextMenuViewtable)v.getTag();
	                	item.visible = true;
	                }
	                break;
	            }
	        	return true;
	        }
	    };
    	
    }
    
    private void clearContextMenu() {
    	adapter.notifyDataSetChanged();
    }
    
}